﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Argument.Models;
using Argument.Setup;
using System.Data.SqlClient;

namespace Argument.Setup
{
    public class RevisionsInitializer : IDatabaseInitializer<ArgumentDbContext>
    {
        private DropCreateDatabaseAlways<ArgumentDbContext> dropper;

        public RevisionsInitializer() {
            dropper = new DropCreateDatabaseAlways<ArgumentDbContext>();
        }

        public void InitializeDatabase(ArgumentDbContext context)
        {
            dropper.InitializeDatabase(context);

            using (SqlConnection conn = new SqlConnection(context.Database.Connection.ConnectionString))
            {
                conn.Open();
                RevisionRunner.RunRevisions(conn);
                conn.Close();
            }

            Seed(context);

            context.SaveChanges();
        }

        protected void Seed(ArgumentDbContext context)
        {

            var assertions = new List<Assertion>
            {
                new Assertion() { Id = 1, Title = "Charleston is a nice place", Body = "Charleston is nice because {{[2]the weather is mild}} and {{[3]the internet is good}}. {{[5]A good internet connection is important}}." },
                new Assertion() { Id = 2, Title = "Charleston weather is mild", Body = "{{[4]Mild weather is sunny or cloudy near 70 degrees}}. Charleston is that way for 60% of the year." },
                new Assertion() { Id = 3, Title = "The internet service in Charleston is good", Body = "My download bandwidth is abc, and my upload is xyz." },
                new Assertion() { Id = 4, Title = "Mild weather is sunny or cloudy near 70 degrees", Body = "Here's a dictionary definition, and something from an encyclopedia." },
                new Assertion() { Id = 5, Title = "It's important to have a good internet connection", Body = "High quality internet connections support both {{[6]productivity}} and {{[7]recreation}}, leading to happiness." },
                new Assertion() { Id = 6, Title = "Fast internet makes people productive", Body = "Here's a study." },
                new Assertion() { Id = 7, Title = "You can have lots of fun with a fast internet connection", Body = "Games are super fun, and you're better at them with a fast connection. Anecdotally, {{[8]I rock at videogames}}, and my {{[3]Charleston internet is good}}." },
                new Assertion() { Id = 8, Title = "Tyler Arehart rocks at videogames", Body = "Here are my amazing stats." }
            };

            assertions.ForEach(i => context.Assertions.Add(i));


            new List<AssertionLink>
            {
                new AssertionLink() { Parent = context.Assertions.Find(1), Child = context.Assertions.Find(2) },
                new AssertionLink() { Parent = context.Assertions.Find(1), Child = context.Assertions.Find(3) },
                new AssertionLink() { Parent = context.Assertions.Find(1), Child = context.Assertions.Find(5) },
                new AssertionLink() { Parent = context.Assertions.Find(2), Child = context.Assertions.Find(4) },
                new AssertionLink() { Parent = context.Assertions.Find(5), Child = context.Assertions.Find(6) },
                new AssertionLink() { Parent = context.Assertions.Find(5), Child = context.Assertions.Find(7) },
                new AssertionLink() { Parent = context.Assertions.Find(7), Child = context.Assertions.Find(8) },
                new AssertionLink() { Parent = context.Assertions.Find(7), Child = context.Assertions.Find(3) }

            }.ForEach(i => context.AssertionLinks.Add(i));

        }


    }
}